<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                     |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Other\Announce;
use \AuctionCore\System\AuctionException;

class AnnounceController extends BaseController {
    public $announce;
    public $emp_id;
    public $emp_name;
    public function __construct() {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
        $this->emp_name = $this->user_info['real_name'];
        $this->announce = new Announce();
        //模板赋值
        $this->assign("emp_id", $this->emp_id);

    }

    public function index() {
        $this->display();
    }

    public function add() {
        $this->display();
    }

    /**
     * 添加公告
     */
    public function add_post() {
        $data['title'] = I("post.ann_title");
        $data['emp_author'] = $this->emp_name;
        $content1 = I('post.content');
        $data['content'] = $this->pase_content($content1);
       //$data['content'] = $data['editorValue'];
        //$this->helpJsonResult(400,$data['content']);die;
       // $isTimedRelease = I("post.isTimedRelease");
        //if ($isTimedRelease == 1) {
            //$data['ispublish'] = 0;
            //$data['timedtime'] = I("post.dingshi");
            //if (empty($data['timedtime'])) {
                //$this->helpJsonResult(400, "发布时间不能为空！");
            //}
        //} else {
        $data['ispublish'] = 1;
        //}
        if (empty($data['title'])) {
            $this->helpJsonResult(400, "公告标题不能为空！");
        }
        if (empty($data['emp_author'])) {
            $this->helpJsonResult(400, "发布人不能为空！");
        }
        if (!$content1) {
            $this->helpJsonResult(400, "公告内容不能为空！");
        }
        $data['emp_id'] = $this->emp_id;
        $data['createtime'] = date("Y-m-d H:i:s", time());
        try {
            $res = M("Announcement")->add($data);
            if ($res) {
                $this->helpJsonResult(200, '公告发布成功！');
            } else {
                $this->helpJsonResult(500, '添加失败！');
            }
        } catch (AuctionException $e) {
            echo $e->getMessage();
        }
    }
    //公告展示页面
    public function announce_list(){
        $page = I("get.p", 1);
        $size = 10;
        $title = I('get.title');
        $params = array();
        $params['title'] = $title;
        //获取list
        $result = $this->announce->get_list($page, $size, $params);

        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }
    //公告展示页面
    public function edit_announce(){
        $id = I('get.ann_id');
        $announce = $this->announce->get_announce($id);
        $announce['content'] = str_replace('</p>',"\n",$announce['content']);
        $announce['content'] = str_replace('<br />',"\n",$announce['content']);
        $announce['content'] = strip_tags($announce['content']);
        $this->assign("announce",$announce);
        $this->display();
    }
    //执行修改
    public function edit_announce_post(){
        $ann_id = I('post.ann_id');
        $data['title'] = I('post.ann_title','','trim');
        $content1 = I('post.content');
        if (!$content1) {
            $this->helpJsonResult(400, "公告内容不能为空！");
        }
        $data['content'] = $this->pase_content($content1);
        $res =  $this->announce->save_announce($ann_id,$data);
        if ($res === false) {
            $this->helpJsonResult(500, '修改失败');
        }else{
            $this->helpJsonResult(200, '修改成功');
        }
    }

    public function pase_content($content1){
        $old_content = explode("\n",$content1);
        //$max_key =  max(array_flip($old_content));
        $content = array_map("trim",$old_content);
        foreach($content as $key => $val){
            if($key == 0) {
                $new_content[] = '<p class="notice-content-text">' . $val . '</p>';
            }else if(empty($content[$key])){
                $new_content[] = '<p style="padding-top:10px;">'.$content[$key].'</p>';
            }else{
                $new_content[] = '<p>'.$val.'</p>';
            }
            /*}else if($key == ($max_key - 1)){
                $new_content[]= '<p class="notice-content-text1">'.$content[$max_key-1].'<br />'.$content[$max_key].'</p>';
            }else{
                $new_content[] = '<p style="padding-top:10px;">'.$val.'</p>';
            }*/
        }
        //array_pop($new_content);
        $content = implode($new_content);
        return $content;
    }
}
